package com.oro.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;

import com.oro.domain.Categoria;
import com.oro.service.StudentService;

public class CourseCategoryConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {

		StudentService studentService = (StudentService) FacesContextUtils
				.getWebApplicationContext(context).getBean("studentService");

		Long id = Long.parseLong(value);
		Categoria cat = studentService.getCategory(id);
		return cat;

	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {

		Categoria cat = (Categoria) value;

		if (cat == null) {
			return ((Long) 0L).toString();
		}
		if (cat.getId() == null) {
			return ((Long) 0L).toString();
		}
		if (((Long) 0L).equals(cat.getId())) {
			return ((Long) 0L).toString();
		}
		return cat.getId().toString();

	}

}
